// pages/search/search.js
Page({

    /**
     * 页面的初始数据
     */
    data: {
        pomeList: [],
        pome1List: [],
        historyList: [],
        authorList: [],
        show: true
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
      this.showHistory()
    },

    getInputName: function(e) {
      console.log(e.detail)
    },

    gotoDetail(e) {
      console.log(e.currentTarget.dataset.index)
      wx.request({
        url: 'http://localhost:3000/detail',
        method:'POST',
        data: e.currentTarget.dataset.index,
        header: {
          'content-type': 'application/x-www-form-urlencoded' // 默认值
        },
        success(res) {
            console.log(res.data);
            let id = res.data.id
            wx.navigateTo({
              url: '../detail/detail?id='+id
            })
        },
        fail: function () {
          console.log('err');
        }
      })
    },

    getAuthor(e){
      // console.log(e.currentTarget.dataset.index)
   
        wx.navigateTo({
          url: `../authorDetail/authorDetail?id=${JSON.stringify(e.currentTarget.dataset.index)}`
        })
    },

    loginForm: function(data) {
        console.log(data.detail.value)
        var name = data.detail.value.name
        console.log(name)
        var self = this
        const openid = wx.getStorageSync('openid')
        console.log(openid)

        wx.request({
          url: 'http://localhost:3000/history',
          method: 'POST',
          data: {
            openid: wx.getStorageSync('openid'),
            name: data.detail.value.name
          },
          header: {
            'content-type': 'application/x-www-form-urlencoded' // 默认值
          },
          success(res) {
            console.log(res.data)
          }
        })

        wx.request({
          url: 'http://localhost:3000/sreachbox',
          method: 'POST',
          data: {name:name},
          header: {
            'content-type': 'application/x-www-form-urlencoded' // 默认值
          },
          success(res) {
            console.log(res.data) // 打印服务器返回的数据
             if(res.data.code === 0) {
               self.setData({
                  pomeList: res.data.result,
                  authorList: res.data.res,
                  pome1List: res.data.res1,
                  show: false
               })
               
             } else {
               wx.showToast({
                 title: '获取失败',
               })
             }
          }
        })
      },
      
      showHistory() {
        var self = this
        wx.request({
          url: 'http://localhost:3000/showhistory',
          success(res) {
            console.log(res.data.result) // 打印服务器返回的数据
             if(res.data.code === 0) {
               self.setData({
                  historyList: res.data.result
               })
             } else {
               wx.showToast({
                 title: '获取失败',
               })
             }
          }
        })
        // this.showHistory()
      },

      onClearHistory(e) {
        // console.log(e.currentTarget.dataset.index)
        wx.showToast({
          title: '已全部清空',
        })
        console.log(wx.getStorageSync('openid'))
        wx.request({
          url: 'http://localhost:3000/deletehistory',
          method:'POST',
          data:{
            openid: wx.getStorageSync('openid')
          },
          header: {
            'content-type': 'application/x-www-form-urlencoded' // 默认值
          },
          success(res) {
              console.log(res.data);
          },
          fail: function () {
            console.log('err');
          }
        })
        this.showHistory()
      },

      getmore(e){
        // console.log(666)
        console.log(e.currentTarget.dataset.index)
        wx.navigateTo({
          url: `../poemmore/pomemore?name=${JSON.stringify(e.currentTarget.dataset.index)}`
        })
      },

      takeHistory(e){
        console.log(e.currentTarget.dataset.index)
        let history = e.currentTarget.dataset.index.history
        console.log(history)
        var self = this
        wx.request({
          url: 'http://localhost:3000/historybox',
          method: 'POST',
          data: {history: history},
          header: {
            'content-type': 'application/x-www-form-urlencoded' // 默认值
          },
          success(res) {
            console.log(res.data) // 打印服务器返回的数据
             if(res.data.code === 0) {
               self.setData({
                  pomeList: res.data.result,
                  authorList: res.data.res,
               })
               console.log('666')
             } else {
               wx.showToast({
                 title: '获取失败',
               })
             }
          }
        })
      },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {
        this.showHistory()
    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {

    }
})